﻿
Partial Class WebForm_ManageRole
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLParent()
            Me.BindGV()
            '判斷是否有找到
            If DirectCast(Master.FindControl("lblHeadText"), Label) IsNot Nothing Then
                DirectCast(Master.FindControl("lblHeadText"), Label).Text = Me.GetLocalResourceObject("Title.Text")
            End If
        End If
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If Me.AddData() = True Then
            Me.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("AddData_Success"))
        End If
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gv.RowCancelingEdit
        Me.gv.EditIndex = -1
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        If e.CommandName = "btnRoleUserMapping" Then
            'Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            'Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            'Me.SearchRoleUserMappingByRole1.Show(key("RoleID"))
        ElseIf e.CommandName = "btnRoleOrganizationMapping" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            Me.SearchRoleOrganizationMappingByRole1.Show(key("RoleID"))
        ElseIf e.CommandName = "btnRoleProgramMapping" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            Me.SearchRoleProgramMappingByRole1.Show(key("RoleID"))
        End If
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            TryCast(e.Row.FindControl("chkActive"), CheckBox).Checked = IIf(DataBinder.Eval(e.Row.DataItem, "Active") = "Y", True, False)
            'TryCast(e.Row.FindControl("AdjustSequence1"), WebForm_AdjustSequence).BindData()
        End If
    End Sub

    Protected Sub gv_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gv.RowEditing
        If Me.gv.Data(e.NewEditIndex)("Builtin") = "Y" Then
            MyObj.ShowMessage("內建角色無法修改")
            e.Cancel = True
            Return
        End If

        Me.gv.EditIndex = e.NewEditIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        If Me.SaveData(e.RowIndex) = True Then
            Me.gv.EditIndex = -1
            Me.BindGV()
            MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_Success"))
        End If
    End Sub

    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub

    Protected Sub SearchRoleProgramMappingByRole1_Loaded(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchRoleProgramMappingByRole1.Loaded
        'Me.SearchRoleProgramMappingByRole1.BindGV()
    End Sub
End Class

Partial Class WebForm_ManageRole
    Private Sub BindDDLParent()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select r.* ")
        sql.AppendFormat("from FD_Role r ")
        sql.AppendFormat("where r.RoleID in ({0}) ", clsAuth.GetRoleList())
        sql.AppendFormat("and r.Virtual = 'N' ")

        Me.ddlParent.DataTextField = "RoleName"
        Me.ddlParent.DataValueField = "RoleID"
        Me.ddlParent.DataSource = dat.GetData(sql)
        Me.ddlParent.DataBind()
    End Sub

    Private Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        '父群組之成員可修改子群組權限
        sql.AppendFormat("select * from (")
        sql.AppendFormat("select r.* ")
        sql.AppendFormat(", r_parent.RoleName as ParentRoleName ")
        sql.AppendFormat("from FD_Role r ")
        sql.AppendFormat("inner join FD_Role r_parent on r.Parent = r_parent.RoleID ")
        sql.AppendFormat("where r_parent.RoleID in ({0}) ", clsAuth.GetRoleList())
        sql.AppendFormat("union ")
        '系統管理者可修改系統管理群組
        sql.AppendFormat("select r.* ")
        sql.AppendFormat(", null as ParentRoleName ")
        sql.AppendFormat("from FD_Role r ")
        sql.AppendFormat("where r.RoleName = 'System' and r.RoleID in ({0})", clsAuth.GetRoleList())
        sql.AppendFormat(") a ")
        sql.AppendFormat(Me.gv.OrderBySQL)

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()
    End Sub

    Private Function AddData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("select r.* from FD_Role r ")
        sql.AppendFormat("where r.RoleName = '{0}' ", Me.txtRoleName.Text)

        If dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0).Rows.Count > 0 Then
            MyObj.ShowMessage(Me.GetLocalResourceObject("AddData_Existed"))
            Return False
        End If

        sql.Length = 0
        sql.AppendFormat("insert FD_Role (RoleName,Parent,Active,BuiltIn,Virtual) select ")
        sql.AppendFormat(" '{0}' ", Me.txtRoleName.Text)
        sql.AppendFormat(",'{0}' ", Me.ddlParent.SelectedValue)
        sql.AppendFormat(",'{0}' ", "Y")
        sql.AppendFormat(",'{0}' ", "N")
        sql.AppendFormat(",'{0}' ", "N")

        Using scope As New Transactions.TransactionScope
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            scope.Complete()
        End Using

        Return True
    End Function

    Private Function SaveData(ByVal rowIndex As Integer) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Dim row As GridViewRow = Me.gv.Rows(rowIndex)
        Dim key As DataKey = Me.gv.DataKeys(rowIndex)

        sql.Length = 0
        sql.AppendFormat("update FD_Role set ")
        sql.AppendFormat(" RoleName='{0}' ", TryCast(row.Cells(2).Controls(0), TextBox).Text)

        sql.AppendFormat(",RoleDescription=@RoleDescription ")
        sql.AddWithValue("RoleDescription", TryCast(row.Cells(3).Controls(0), TextBox).Text)

        sql.AppendFormat(",Active='{0}' ", IIf(TryCast(row.FindControl("chkActive"), CheckBox).Checked, "Y", "N"))
        sql.AppendFormat("where RoleID={0} ", key("RoleID"))

        Using scope As New Transactions.TransactionScope
            dat.AccData(sql)

            scope.Complete()
        End Using

        Return True
    End Function
End Class